//
//  LEOSearchView.swift
//  SinaWeibo
//
//  Created by 李子海LEO on 4/6/16.
//  Copyright © 2016 李子海LEO. All rights reserved.
//

import UIKit

class LEOSearchView: UIView {
    
    
    @IBOutlet weak var textField: UITextField!
    
    @IBOutlet weak var cancelButton: UIButton!
    
    @IBOutlet weak var rightConstraints: NSLayoutConstraint!
    //MARK:创建 searchView
    
    class func creatSearchView () -> LEOSearchView{
        

        let searchView = NSBundle.mainBundle().loadNibNamed("LEOSearchView", owner: nil, options: nil).last as! LEOSearchView
        
        
        return searchView
    }
    
    override func awakeFromNib() {
        
        self.frame=CGRect(x: 0, y: 0, width:UIScreen.mainScreen().bounds.width , height: self.bounds.height)
        
        textField.leftView=UIImageView.init(image: UIImage.init(named: "searchbar_textfield_search_icon"))
     
        textField.leftView?.frame=CGRectMake(0, 0, self.bounds.height, self.bounds.height)
        
        //需要设置leftView为始终显示
        self.textField.leftViewMode = .Always
        
        //设置图片显示模式
        self.textField.leftView?.contentMode = .Center
        
       // self.textField.leftView?.sizeToFit()设置之后,我们设置的 frame 将会失效
 
    }
    
    //MARK:点击 TextFiled 触发事件
    @IBAction func clickTextFiled(sender: AnyObject) {
        
        //修改 textField 右边的约束->需要拿到右边约束对象,并执行约束动画
        
        rightConstraints.constant=cancelButton.frame.width
        
        LEOSearchView.animateWithDuration(0.25) { () -> Void in
            
            self.layoutIfNeeded()
            
        }
        
    
    }
    
    
    //MARK:点击 cancelButton 触发事件
    @IBAction func clickButton(sender: AnyObject) {
        
        textField.resignFirstResponder()
        
        
        rightConstraints.constant=0
        
        LEOSearchView.animateWithDuration(0.25) { () -> Void in  //-->为什么需要() -> Void

            self.layoutIfNeeded()
            
        }
    }

}


